﻿using System;
using System.Collections.Generic;
using System.Text;

namespace IceCS.Puke.Client
{
    /// <summary>
    /// 游戏平台
    /// </summary>
    public class Launch
    {
        /// <summary>
        /// 运行游戏
        /// </summary>
        public static void RunGame()
        {
            bool isContinueGame = true;
            while (isContinueGame)
            {
                PrintGameDescription();

                var players = GetPlayers();
                Game game = new Game();
                game.SetPlayerList(players);
                Player winner = game.PlayGame();
                PrintGameResult(winner.Name);
                string choice = GameHelper.GetRightInput("游戏结束，您是否继续(yes or no)", new string[] { "y", "yes", "n", "no" });
                isContinueGame = choice.ToLower() == "y" || choice.ToLower() == "yes";
                if (isContinueGame)
                {
                    Console.WriteLine("开始新的一轮游戏");
                }
            }
        }
        /// <summary>
        /// 输出游戏描述
        /// </summary>
        private static void PrintGameDescription()
        {
            var sb = new StringBuilder();
            sb.AppendLine("卡莉卡牌：一种新奇的卡牌玩法！");
            sb.AppendLine("胜利规则：当您手里的七张牌花色一样时，您就胜利啦");
            sb.AppendLine("---------------------------------");
            var result = sb.ToString();
            Console.WriteLine(result);
        }

        /// <summary>
        /// 获得玩家列表和对玩家命名
        /// </summary>
        /// <returns></returns>
        private static List<Player> GetPlayers()
        {
            string input = GameHelper.GetRightInput("请输入玩家数量：（2-7）：", new[] { "2", "3", "4", "5", "6", "7" });
            int choices = int.Parse(input);

            var players = new List<Player>(choices);
            for (int i = 1; i <= choices; i++)
            {
                var defaultName = $"player{i}";
                Console.WriteLine($"玩家 {i}，请输入您的名字(直接回车(enter)则为 {defaultName}):");

                var name = Console.ReadLine();

                if (string.IsNullOrEmpty(name))
                {
                    name = defaultName;
                }
                else
                {
                    name = name.Trim();
                }
                players.Add(new Player(name));
            }
            return players;
        }
        /// <summary>
        /// 打印游戏结果
        /// </summary>
        /// <param name="winnerName"></param>
        private static void PrintGameResult(string winnerName)
        {
            Console.WriteLine($"玩家 【{winnerName}】 赢得了比赛 ^-^");
        }
    }
}
